package com.example.huichaoliu;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.huichaoliu.data.UserInfo;
import com.rederxu.farmer.BaseActivity;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;
import com.rederxu.tools.StringUtils;
import com.source.utils.Utils;

//尽量让所有activity都继承自BaseActivity，可以有诸多好处，请仔细阅读BaseActivity的文档
public class LoginActivity extends BaseActivity implements OnClickListener {

	private EditText et_mobile = null;
	private EditText et_password = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.left_button:
			finish();
			break;
		case R.id.right_button:
			startActivity(RegistActivity.class);
			break;
		case R.id.btn_login:
			String pwd_str = et_password.getText().toString();
			String mobile_str = et_mobile.getText().toString();
			if(StringUtils.isBlank(mobile_str) || mobile_str.length() != 11){
				showToast("请输入合法手机号");
				return;
			}
			if(pwd_str.length() < 6 || pwd_str.length() > 16){
				showToast("请输入6~16位密码");
				return;
			}
			try{
				JSONObject jo = new JSONObject();//sendregisterSMS.action
				jo.put("user", mobile_str);
				jo.put("pass", Utils.stringToMD5(pwd_str));
				Bundle bundle = new Bundle();
				bundle.putString("request", Utils.requestParamsEncode(jo));
				HTTPRequest.POST(this, Config.REQUEST_URLS.URL_USER_LOGIN, bundle, new onRequestOverListener() {
					@Override
					public void onRequestOver(String result) {
						try{
							JSONObject jo = new JSONObject(result);
							if(jo.getInt("code") != 0){
								showToast(jo.getString("message"));
								return;
							}
							jo = jo.getJSONObject("response");
							UserInfo.initData(jo, LoginActivity.this);
							finish();
						}catch(JSONException e){
							e.printStackTrace();
						}
					}
				});
			}catch(Exception e){
				
			}
			break;
		case R.id.tv_forget_password:
			startActivity(FindPasswordActivity.class);
			break;
		}
	}

	@Override
	public void init() {
		((TextView) findViewById(R.id.title_text)).setText("登  录");
		((Button) findViewById(R.id.right_button)).setText("注册");
		((Button) findViewById(R.id.right_button)).setVisibility(View.VISIBLE);
		findViewById(R.id.left_button).setOnClickListener(this);
		findViewById(R.id.right_button).setOnClickListener(this);
		findViewById(R.id.tv_forget_password).setOnClickListener(this);
		findViewById(R.id.btn_login).setOnClickListener(this);
		
		et_mobile = (EditText) findViewById(R.id.et_mobile);
		et_password = (EditText) findViewById(R.id.et_password);
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetworkChanged(boolean isMobNetConnected,
			boolean isWifiConnected) {
		// TODO Auto-generated method stub

	}

}
